package top.soulblack.purchasesale.controller.stock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.Supplier;
import top.soulblack.purchasesale.service.SupplierService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * create by 小川
 */

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @RequestMapping("addSupplier")
    @ResponseBody
    public Result<Boolean> addSupplier(Supplier supplier) {
        // 判断是否存在当前颜色
        if (supplierService.existSupplier(supplier.getName())) {
            return Result.error(SUPPLIER_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (supplier.getSort() > 100 || supplier.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        supplierService.addSupplier(supplier);
        return Result.success(true);
    }

    @RequestMapping("/deleteSupplier")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteSupplier(Integer supplierId) {
        if (supplierService.deleteSupplier(supplierId)) {
            return Result.success(true);
        }
        return Result.error(SUPPLIER_DELETE);
    }

    @ResponseBody
    @RequestMapping("/editSupplier")
    public Result<Boolean> editSupplier(Supplier supplier){
        if(supplierService.updateSupplier(supplier)){
            return Result.success(true);
        }
        return Result.error(SUPPLIER_EDIT_ERROR);
    }
}
